// pages/reward/list/list.js
import {
  salary,
  street
} from '../../../utils/base-data.js'
const config = getApp().globalData.config
const T = getApp().globalData.T
let token = ''
Page({

  /**
   * 页面的初始数据
   */
  data: {
    key: '',
    index: '',
    isHaveHalfResume: true,
    resumeList: [],
    posList: [],
    resumePage: 1,
    resumeSize: 10,
    isLoadedResume: false,
    isFirstResumeTip: true,
    posPage: 1,
    posSize: 10,
    isLoadedPos: false,
    isFirstPosTip: true,
    tabs: [{
      key: 'tab1',
      title: '悬赏简历',
    },
    {
      key: 'tab2',
      title: '悬赏职位',
    }
    ],
    isFilterPositon: false, // 是否点击期望职位筛选
    isOpen: false,
    selectArr: [{
      checked: false
    }, {
      checked: false
    }, {
      checked: false
    }, {
      checked: false
    }], // 职位-行业-地点-薪资
    filterPos: '',
    filterStreet: '',
    filterIndustryId: '',
    filterSalaryId: '',
    filterIndustryList: [],
    filterStreetList: [],
    filterSalaryList: [],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    token = getApp().globalData.token;
    this.setData({
      isIos: getApp().globalData.platform == 'ios' ? true : false
    })
    this.initData();
  },

  onShow: function () {
    this.loadRewardResume();
    this.loadRewardPos();
  },

  initData() {
    let industryL = getApp().globalData.industryList;
    console.log(industryL)
    industryL.forEach(m => { // 往对象里添加一个值 
      this.data.filterIndustryList.push(Object.assign(m, {
        checked: false,
        type: 1
      }))
    })
    street.forEach(m => { // 往对象里添加一个值 
      this.data.filterStreetList.push(Object.assign(m, {
        checked: false,
        type: 2
      }))
    })
    salary.forEach(m => { // 往对象里添加一个值 
      this.data.filterSalaryList.push(Object.assign(m, {
        checked: false,
        type: 3
      }))
    })
  },

  onTabsChange(e) {
    const {
      key
    } = e.detail;
    const index = this.data.tabs.map((n) => n.key).indexOf(key);
    this.setData({
      key,
      index,
    });
  },

  onSwiperChange(e) {
    const {
      current: index,
      source
    } = e.detail
    const {
      key
    } = this.data.tabs[index]

    if (!!source) {
      this.setData({
        key,
        index,
      })
    }
  },

  toast(text) {
    T.toast({
      id: '#toast',
      text: text
    });
  },

  loadRewardResume(name = '', industryId = '', location = '', salaryRange = '', isFilterData = false) {
    if (isFilterData) { // 重置数据并重新装填数据
      this.data.resumePage = 1;
      this.resumeList = [];
    }
    let reqParam = T.delSpaceKv({
      name: name,
      industryId: industryId,
      location: location,
      salaryRange: salaryRange
    });
    Object.assign(reqParam, {
      token: token,
      page: this.data.resumePage,
      size: this.data.resumeSize
    })

    T.req({
      loadTitle: '加载中...',
      url: config.api + 'reward/resume_list',
      method: 'GET',
      data: reqParam,
      success: (res) => {
        if (res.rowCount == this.data.resumeList.length) {
          this.data.isLoadedResume = true;
          return;
        }
        let list = JSON.parse(JSON.stringify(res.list));
        list.map((m) => {
          m.position = m.positionName;
          m.headImg = m.resumeImg;
        })
        this.setData({
          resumeList: this.data.resumeList.concat(list)
        })
      }
    })
  },


  loadRewardPos() {
    T.req({
      loadTitle: '加载中...',
      url: config.api + 'reward/position_list',
      method: 'GET',
      data: {
        token: token,
        page: this.data.posPage,
        size: this.data.posSize
      },
      success: (res) => {
        let _temArr = [];
        let list = JSON.parse(JSON.stringify(res.list));
        if (res.rowCount == this.data.posList.length) {
          this.data.isLoadedPos = true;
          return;
        }
        list.forEach((ele, i) => {
          _temArr.push({
            label_1: ele.redPacket ? true : false,
            label_2: !ele.employmentType ? true : false,
            label_3: !!ele.employmentType ? true : false,
            id: ele.positionId,
            workYear: ele.candidateRequire,
            name: ele.name,
            salary: ele.salaryRange,
            type: 0,
            education: ele.education,
            company: ele.company,
            area: ele.street,
            status: ele.mark,
            deadline: ele.deadline,
            reward: ele.reward,
            rewardId: ele.rewardId
          })
        });
        this.setData({
          posList: this.data.posList.concat(_temArr)
        });
      }
    })
  },

  onScrollLoadResume() {
    if (this.data.isLoadedResume) {
      if (this.data.isFirstResumeTip) {
        this.toast('已经加载完全部数据了');
      }
      this.data.isFirstResumeTip = false;
      return;
    }
    this.data.resumePage += 1;
    this.loadRewardResume();
  },

  onScrollPos() {
    if (this.data.isLoadedPos) {
      if (this.data.isFirstPosTip) {
        this.toast('已经加载完全部数据了');
      }
      this.data.isFirstPosTip = false;
      return;
    }
    this.data.posPage += 1;
    this.loadRewardPos();
  },

  onResumeDetail(e) {
    let id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '../resume/details?id=' + id,
    })
  },

  onPosDetail(e) {
    let id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '../position/details?id=' + id,
    })
  },

  /**
   * 加载需要筛选的源数据
   */
  loadFilterData(index) {
    let _temArr = [];
    index = Number(index);
    switch (index) {
      case 1:
        _temArr = this.data.filterIndustryList;
        console.log(_temArr)
        break;
      case 2:
        _temArr = this.data.filterStreetList;
        break;
      case 3:
        _temArr = this.data.filterSalaryList;
        break;
    }
    this.setData({
      filterList: _temArr
    });
  },

  resetNavSelectStatus() {
    this.data.selectArr.map(m => m.checked = false);
  },

  onFilterNav(e) {
    let index = e.currentTarget.dataset.index;
    this.data.selectArr.map((m, i) => {
      if (i == index) {
        // 判断当前nav是否被选中
        if (m.checked) { // 关闭
          m.checked = false;
          this.setData({
            isOpen: false
          });
        } else { // 打开
          let isFilterPositon = index == 0 ? true : false;
          m.checked = true;
          this.loadFilterData(index);
          this.setData({
            isOpen: true,
            isFilterPositon
          });
        }
      } else {
        // 重置其他nav选中状态
        m.checked = false;
      }
    })
  },

  // 防止触发代父元素点击事件
  onTem() {

  },

  onCloseFilter(e) {
    console.log(e)
    let role = e.currentTarget.dataset.role;
    if (role == 'parent') {
      this.setData({
        isOpen: false
      });
      this.resetNavSelectStatus();
    }
  },

  onInputFilterPos(e) {
    this.setData({
      filterPos: e.detail.value
    });
  },

  onSelectPosition() {
    let name = this.data.filterPos;
    this.loadRewardResume(this.data.filterPos, this.data.filterIndustryId, this.data.filterStreet, this.data.filterSalaryId);
    this.setData({
      isOpen: false
    });
    this.resetNavSelectStatus();
  },

  onFilterData(e) {
    let index = e.currentTarget.dataset.index;
    let type = e.currentTarget.dataset.type;
    let title = e.currentTarget.dataset.title;
    let value = e.currentTarget.dataset.value;

    if (type == 1) { // 行业
      this.data.filterIndustryList.map((m, i) => {
        if (index == i) {
          m.checked = true;
        } else {
          m.checked = false;
        }
      })
      this.data.filterIndustryId = value;
      this.setData({
        filterList: this.data.filterIndustryList
      })
    }
    if (type == 2) { // 工作
      this.data.filterStreetList.map((m, i) => {
        if (index == i) {
          m.checked = true;
        } else {
          m.checked = false;
        }
      })
      this.data.filterStreet = title;
      this.setData({
        filterList: this.data.filterStreetList
      })
    }
    if (type == 3) { // 薪资
      this.data.filterSalaryList.map((m, i) => {
        if (index == i) {
          m.checked = true;
        } else {
          m.checked = false;
        }
      })
      this.data.filterSalaryId = value;
      this.setData({
        filterList: this.data.filterSalaryList
      })
    }
    console.log('index:' + index, 'type:' + type, 'title:' + title, 'value:' + value)
    this.resetNavSelectStatus();
    this.loadRewardResume(this.data.filterPos, this.data.filterIndustryId, this.data.filterStreet, this.data.filterSalaryId, true);
  },


  // onTopublish() {
  //   wx.navigateTo({
  //     url: '../publish/resume',
  //   })
  // }

})